package leetcode.binaryTree;

import utils.printer.BinaryTrees;
import datastructure.tree.BinarySearchTree;
import datastructure.tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Description: https://leetcode.cn/problems/maximum-depth-of-binary-tree/
 * 给定一个二叉树，找出其最大深度。
 *
 * 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
 *
 * 说明: 叶子节点是指没有子节点的节点。
 * @Author Ammar
 * @Create 2023/3/29 22:59
 */
public class _104_二叉树的最大深度 {

    public static int maxDepth(TreeNode<Integer> root) {
        if (root == null) return 0;
        int levelSize = 1;
        int height = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode<Integer> node = root;
        while (queue.size() != 0) {
            node = queue.poll();
            levelSize--;
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
            if (levelSize == 0) {
                levelSize = queue.size();
                height++;
            }
        }
        return height;
    }


    public static void main(String[] args) {
        Integer[] data = new Integer[]{
                7, 4, 9, 2, 5, 8 ,11, 3, 12, 1
        };
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            binarySearchTree.add(data[i]);
        }
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        System.out.println(maxDepth(binarySearchTree.getRoot()));
    }
}
